package com.tju.backend.plugin.t_io_server.handler;

import cn.hutool.http.HttpRequest;
import com.tju.backend.entity.TStereoscopicWarehouse;
import com.tju.backend.plugin.common.Push;
import com.tju.backend.plugin.common.SPPacket;
import com.tju.backend.plugin.common.intf.AbsCommonBsHandler;
import com.tju.backend.utils.hex.HexStringUtil;
import com.tju.backend.utils.result.ResultEnum;
import com.tju.backend.utils.result.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;

import java.util.ArrayList;
import java.util.List;

import static com.tju.backend.plugin.common.GlobalCall.sw;

/**
 * Plc信号处理
 *
 * @Author Lei
 * @Date 2021/12/8 14:50
 */
@Slf4j
@Component
public class PlcSignal extends AbsCommonBsHandler {
    private String last = "0"; //记录状态

    //转成2进制
    public static String byteArrToBinStr(byte[] b) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            result.append(Long.toString(b[i] & 0xff, 2) + ",");
        }
        return result.toString().substring(0, result.length() - 1);
    }

    public static void main(String[] args) {
        String value = "01";
        String last = "00";
        int count = 8;
        int valueLength = value.length();
        int lastLength = last.length();
        List<Integer> values = new ArrayList<Integer>();
        if (valueLength != count) {
            for (int i = 0; i < count - valueLength; i++) {
                value = "0" + value;
            }
        }
        if (lastLength != count) {
            for (int i = 0; i < count - lastLength; i++) {
                last = "0" + last;
            }
        }
        if (last.equals(value)) {
            return;
        } else {
            for (int countIndex = count - 1; countIndex >= 0; countIndex--) {
                char c = value.charAt(countIndex);
                char d = last.charAt(countIndex);
                if (c != d) {
                    values.add(countIndex);
                }
            }
            for (Integer integer : values) {
                char c = value.charAt(integer);
                switch (integer) {
                    case 7:
                        if (c == '1') {
                            System.err.println(0);
                        } else if (c == '0') {
                            System.err.println(10);
                        }
                        break;
                    case 6:
                        if (c == '1') {
                            System.err.println(1);
                        }
                        break;
                    case 5:
                        if (c == '1') {
                            System.err.println(2);
                        }
                        break;
                    case 4:
                        if (c == '1') {
                            System.err.println(3);
                        }
                        break;
                    case 3:
                        if (c == '1') {
                            System.err.println(4);
                        }
                        break;
                    case 2:
                        if (c == '1') {
                            System.err.println(5);
                        }
                        break;
                    case 1:
                        if (c == '1') {
                            System.err.println(6);
                        }
                        break;
                    case 0:
                        if (c == '1') {
                            System.err.println(7);
                        }
                        break;
                }
            }
        }
        System.err.println(11);
    }

    @Override
    public void handler(SPPacket packet, String bsBody, ChannelContext channelContext) throws Exception {
        try {
            String msg = byteArrToBinStr(packet.getBody());
            String[] split = msg.split(",");
            String value = split[0];//拿到前8位

            int count = 8;
            int valueLength = value.length();
            int lastLength = last.length();
            List<Integer> values = new ArrayList<Integer>();
            if (valueLength != count) {
                for (int i = 0; i < count - valueLength; i++) {
                    value = "0" + value;
                }
            }
            if (lastLength != count) {
                for (int i = 0; i < count - lastLength; i++) {
                    last = "0" + last;
                }
            }

            if (last.equals(value)) {
                return;
            } else {
                for (int countIndex = count - 1; countIndex >= 0; countIndex--) {
                    char c = value.charAt(countIndex);
                    char d = last.charAt(countIndex);
                    if (c != d) {
                        values.add(countIndex);
                    }
                }
                log.info("上一次的数据" + value);
                log.info("解析的数据" + value);
                last = value;
                for (Integer integer : values) {
                    char c = value.charAt(integer);
                    switch (integer) {
                        case 7:
                            if (c == '1') {
                                http("GoToTheBoxArea");//调用agv来接箱子
                            } else if (c == '0') {
                                http("GoToTheUpperBoxArea");//调用agv去上箱子
                            }
                            break;
                        case 6:
                            if (c == '1') {
                                http("TakeTheBoxToTheRestArea");//调用agv回休息区
                            }
                            break;
                        case 5:
                            if (c == '1') {
                                System.err.println(2);
                            }
                            break;
                        case 4:
                            if (c == '1') {
                                System.err.println(3);
                            }
                            break;
                        case 3:
                            if (c == '1') {
                                Push.push(ResultUtil.success(ResultEnum.STEREOSCOPIC_WAREHOUSEONE, "B01"));
                            }
                            break;
                        case 2:
                            if (c == '1') {
                                TStereoscopicWarehouse ws = sw.selectAllstatus(2);
                                sw.updateStatus( 1,ws.getId());
                                Push.push(ResultUtil.success(ResultEnum.STEREOSCOPIC_WAREHOUSEONE, "B02"));
                            }
                            break;
                        case 1:
                            if (c == '1') {
                                Push.push(ResultUtil.success(ResultEnum.STEREOSCOPIC_WAREHOUSEONE, "B03"));
                            }
                            break;
                        case 0:
                            if (c == '1') {

                                Push.push(ResultUtil.success(ResultEnum.STEREOSCOPIC_WAREHOUSEONE, "B04"));
                            }
                            break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    public void http(String url) {
        HttpRequest.get("192.168.3.148:80/agv/" + url)
                .body("")
                .execute().body();
    }

}
